<?php
/**
 * 公用会员信息
 * @return
 * @author yuncran@126.com
 */

namespace app\services;


use app\model\ConfigModel;
use app\model\UserBlacklistModel;
use app\model\UserCensusCountModel;
use app\model\UserModel;
use GatewayClient\Gateway;
use think\Db;

class UserService
{

    /**
     * 增加会员统计的信息
     * @return void
     * @author yuncran@126.com
     */
    public static function UserCensusCount($user_id, $type, $field)
    {
        /** 增加数据 */
        if ($type == 'add') {
            UserCensusCountModel::where(['user_id' => $user_id])->inc($field, 1)->update();
        } else {
            UserCensusCountModel::where(['user_id' => $user_id])->dec($field, 1)->update();
        }
        return true;
    }


    /**
     * 会员加入黑名单
     * @param $data
     * @return UserBlacklistModel|\think\Model
     * @author yuncran@126.com
     */
    public static function addUserBlack($data = [])
    {
        return UserBlacklistModel::create($data);
    }

    /**
     * 黑名单列表
     * @param $user_id
     * @param $data
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @author yuncran@126.com
     */
    public static function getUserBlackList($user_id, $data)
    {
        $list = UserBlacklistModel::field(['id', 'user_id', 'to_user_id', 'create_time'])
            ->where(['user_id' => $user_id])->when($data, function ($query) use ($data) {
                if (!empty($data['keywords'])) {
                    $user_ids = UserModel::where('nickname', 'like', "%{$data['keywords']}%")->column('id');
                    return $query->whereIn('to_user_id', $user_ids);
                }
            })
            ->paginate($data['limit'])->each(function ($item) {
                return $item;
            });
        return $list;
    }


    /**
     * 删除黑名单
     * @param $where
     * @return bool
     * @author yuncran@126.com
     */
    public static function userBlackDel($where = [])
    {
        return UserBlacklistModel::where($where)->delete();
    }

    /**
     * 检测会员是否在线 | 在线返回 normal
     * @param $user_id
     * @return void
     * @author yuncran@126.com
     */
    public static function isUserOnline($user_id)
    {
        Gateway::$registerAddress = config('gateway.registerAddress');
        $onlineList = Gateway::getAllUidList();
        $online = in_array($user_id, $onlineList) ? ['user_id' => $user_id, 'is_online' => 'normal'] : ['user_id' => $user_id, 'is_online' => 'hidden'];
        return $online;
    }

    public static function get_is_read(string $table, array $where)
    {
        return \think\facade\Db::name($table)->where($where)->find();
    }

    /**
     * @return void
     * @author yuncran@126.com
     */
    public static function is_read(string $table, array $where)
    {
        return \think\facade\Db::name($table)->where($where)->update(['is_read' => 1, 'update_time' => time()]);
    }


    public static function getOn(string $table, $user_id)
    {
        return \think\facade\Db::name($table)->where(['user_id' => $user_id])->where(function ($query) use ($table) {
            if ($table == 'trends_user_like') {
                $query->where('status', 1);
            }
        })->where(['is_read' => 0])->find();
    }

    /**
     * @return void
     * @author yuncran@126.com
     */
    public static function all_is_read(string $table, $user_id)
    {
        return \think\facade\Db::name($table)->where(['user_id' => $user_id])->where(function ($query) use ($table) {
            if ($table == 'trends_user_like') {
                $query->where('status', 1);
            }
        })->where(['is_read' => 0])->update(['is_read' => 1, 'update_time' => time()]);
    }


    /**
     *
     * @return void
     * @author yuncran@126.com
     */
    public static function getFilter($inputText)
    {
        $response = ConfigModel::getSensitive(['id' => 25]);
        $text = $inputText;
        if (!empty($response['value'])) {
            $sensitiveWords = explode(',', $response['value']);
            $text = filterSensitiveWords($inputText, $sensitiveWords);
        }
        return $text;
    }


    /**
     * 查询Ip
     * @return void
     * @author yuncran@126.com
     */
    public static function getIp($ip)
    {
        $response = ConfigModel::getSensitive(['id' => 6]);
        $rest = file_get_contents("http://ip-api.com/json/$ip");
        $data = json_decode($rest, true);
        if (empty($data)) return false;
        if ($data['countryCode'] == 'CN') {
            if (!empty($response['value'])) {
                $arrIp = explode(',', $response['value']);
                if (!in_array($ip, $arrIp)) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }

}